package org.itboys.mobile.controller.mobile.colletion;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.util.MobileSignUtils;
import org.itboys.mobile.service.mongo.mobile.collection.MdlMobileCollectionService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2017/12/4.
 * @author 朱坤涛
 * 收藏
 */
@Controller
@RequestMapping("/mdl/mobile/collection")
@Api(value = "collection",description = "收藏")
public class MdlMobileCollectionController extends BaseController{

    @Autowired
    private MdlMobileCollectionService collectionService;

    /**
     * 查询用户所有收货地址
     * @param request
     * @param response
     * @param type
     * @param pageSize
     * @param pageNumber
     * @param sort
     * @param order
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/select", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】获取收藏的物品。商品，都要分页")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required=true, name="type", value="收藏的类型 1商品")@RequestParam(value = "type", required = true)Integer type,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("type",type);
            param.put("pageSize",pageSize);
            param.put("pageNumber",pageNumber);
            param.put("sort",sort);
            param.put("order",order);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            Long memberId = AccountIdHoldUtils.getMemberId();
            List<Map<String,Object>> result =collectionService.selectCollection(request,memberId,type);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/collection/select----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 收藏或者取消收藏
     * @param request
     * @param response
     * @param type
     * @param otherId
     */
    @RequestMapping(value="/save", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】收藏或者取消收藏")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(required=true, name="type", value="收藏类型 1商品")@RequestParam(value = "type", required = true)Integer type,
                     @ApiParam(required=true, name="otherId", value="被收藏（商品）的id")@RequestParam(value = "otherId", required = true)Long otherId,
                     @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                     @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("type",type);
            param.put("otherId",otherId);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            Long memberId = AccountIdHoldUtils.getMemberId();
            collectionService.collection(memberId,type,otherId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/collection/save----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }

    }

}
